<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\validate\TableFields as validate;
use app\admin\model\TableFields as model;
use think\App;
use think\facade\Hook;

class TableFields extends Base{
    public function __construct(App $app = null){
        parent::__construct($app);
        $this->validate = new validate;
        $this->model = new model;
        $this->getTablesAttr();
    }

    /**
     * 获取列表数据
     * @param array $where
     * @param string $sort
     * @param int $limit
     * @return \think\Response
     * @throws \think\exception\DbException
     */
    public function index($where=[],$sort='',$limit=8){
        $where = $this->_where($where);
        $order = $this->_order($sort);
        $list = $this->model->getList($where,$order,$limit);
        return $this->__s('',$list);
    }

    /**
     * 保存数据
     * @param int $id
     * @return \think\Response
     */
    public function save($id=0){
        $data = $this->request->post();
        if(!$this->validate->check($data)){
            return $this->__e($this->validate->getError());
        }
        if(!$this->model->save($data)){
            return $this->__e('操作失败');
        }
        $sql = $this->model->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>$id?'更新':'添加'."{$this->model->getTableComment()}，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('操作成功');
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function delete($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        if(!$info->delete()){
            return $this->__e('删除失败');
        }
        $sql = $info->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>"删除{$this->model->getTableComment()}，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('删除成功');
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function read($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    public function edit($id){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function update($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        $field = json_encode($this->request->put('field'));
        if($field == $info->table_fields_sort){
            return $this->__e('','','2022');
        }
        $info->table_fields_sort=$field;
        if(!$info->save()){
            return $this->__e('操作失败');
        }
        $sql = $info->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>"更新{$this->model->getTableComment()}，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('操作成功');
    }

    /**
     * @param int $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @route('admin/table/unshow','get')
     */
    public function getUnshow($id=0){
        $info = $this->model->field('unshow')->find($id);
        $data = $info['unshow']?explode(',',$info['unshow']):[];
        return $this->__s('',$data);
    }

    /**
     * @param int $id
     * @param array $unshow
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @route('admin/table/unshow','post')
     */
    public function setUnshow($id=0,$list=[]){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e('表不存在');
        }
        $info->unshow = implode(',', $list);
        if(!$info->save()){
            return $this->__e('操作失败');
        }
        $sql = $info->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>"更新{$info['table_name']}字段显示排序，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('操作成功');
    }
}